package com.longfor.dragonshard.controller;

import com.longfor.dragonshard.dto.*;
import com.longfor.dragonshard.dto.account.LoginDTO;
import com.longfor.dragonshard.dto.demo.DemoAddDTO;
import com.longfor.dragonshard.entity.Demo;
import com.longfor.dragonshard.service.IDemoAccountService;
import com.longfor.dragonshard.service.ILoginService;
import com.longfor.dragonshard.service.IValidateService;
import com.longfor.drogon.common.bean.LFResultBean;
import com.longfor.drogon.common.exception.LFValidationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Demo login
 *
 * @author chi.zhang
 * @version v1.0
 * @create 2017-10-05 下午2:28
 **/
@CrossOrigin
@RestController
public class LoginController {
    private Logger logger = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private IValidateService validateService;
    @Autowired
    private ILoginService loginService;

    /*
      --------------------------------------------------------
      -- web接口
      --------------------------------------------------------
     */
    /**
     * 获取验证码
     *
     * @param request
     * @return
     */
    @GetMapping(value = "/web/api/getValidate")
    public LFResultBean getValidate(HttpServletRequest request) {
        return new LFResultBean<>(validateService.getValidate(request));
    }
    /**
     * 登录
     *
     * @param loginDTO
     * @param errors
     * @return
     */
    @PostMapping(value = "/web/api/login")
    public LFResultBean add(@Valid @RequestBody LoginDTO loginDTO, Errors errors) {
        //服务端数据校验
        if (errors.hasErrors()) {
            throw new LFValidationException(errors);
        }

        return new LFResultBean<>(loginService.doLogin(loginDTO));
    }


}
